﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Shop.Web.Controls
{
    public partial class FilterItem : System.Web.UI.UserControl
    {
        private SortedList<int,string> ds;
        private string cssClass;
        private string queryType;
        private string startValue = string.Empty;

        public SortedList<int, string> DataSourse
        {
            get { return ds; }
            set { ds = value; }
        }

        public string QueryType
        {
            get { return queryType; }
            set { queryType = value; }
        }

        public string StartValue
        {
            get { return startValue; }
            set { startValue = value; }
        }

        protected string SubQuery(string str, string val)
        {
            if (StartValue.Contains(QueryType))
            {
                int i = StartValue.IndexOf("&", StartValue.IndexOf(QueryType));
                if (i < 1)
                    i = StartValue.Length - StartValue.IndexOf(QueryType);
                else
                {
                    i = i - StartValue.IndexOf(QueryType);
                }
                string s = StartValue.Substring(StartValue.IndexOf(QueryType),i);
                if (s.Contains("," + str + ",") || s.EndsWith("," + str))
                {
                    val = "<b>"+val+"</b>";
                    s = s.Replace("," + str, "");
                }
                else
                {
                    s = s + "," + str;
                }
                return "<a href=\"" + StartValue.Replace(StartValue.Substring(StartValue.IndexOf(QueryType), i), s) + "\">" + val + "</a>";
            }
            return "<a href=\""+StartValue+"&" + QueryType + "=," + str+"\">" + val + "</a>";
        }

        protected override void OnPreRender(EventArgs e)
        {
            rprMain.DataSource = DataSourse;
            rprMain.DataBind();
            //hdnValues.Value = StartValue;
        }

        protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HtmlGenericControl chbCheck = (HtmlGenericControl)e.Item.FindControl("chbCheck");
            if (chbCheck != null)
            {
                //chbCheck.Attributes["valObj"] = hdnValues.ClientID;
                if (StartValue.Contains("," + chbCheck.Attributes["value"].ToString() + ",") || StartValue == "," + chbCheck.Attributes["value"].ToString() || StartValue.EndsWith("," + chbCheck.Attributes["value"].ToString()))
                {
                    chbCheck.Attributes["class"] = "checked_item_b";
                }
            }
        }
    }
}